package com.cola.service;

import com.alibaba.fastjson.JSON;
import com.cola.dao.CommonDao;
import com.cola.entity.Area;
import com.cola.entity.City;
import com.cola.enums.CommonType;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created by Max on 2017/8/24.
 * 通用Service
 */
public class CommonService {
    private static volatile CommonService instance = null;
    public static CommonService getInstance() {
        if (instance == null) {
            synchronized (CommonService.class) {
                if (instance == null) {
                    instance = new CommonService();
                }
            }
        }
        return instance;
    }

    /**
     * 获取地区列表
     * @return
     */
    public List<Map<String,Object>> getAreaList(){
        return CommonDao.getInstance().getCommonContentByType(CommonType.AREA.getDesc());
    }

    /**
     * 获取品牌列表
     * @return
     */
    public List<Map<String,Object>> getBrandList(){
        return CommonDao.getInstance().getCommonContentByType(CommonType.BRAND.getDesc());
    }

    /**
     * 获取商品类型列表
     * @return
     */
    public List<Map<String,Object>> getGoodsTypeList(){
        return CommonDao.getInstance().getCommonContentByType(CommonType.GOODS_TYPE.getDesc());
    }

    /**
     * 获取商品类型列表
     * @return
     */
    public List<Map<String,Object>> getGoodsUnitList(){
        return CommonDao.getInstance().getCommonContentByType(CommonType.GOODS_UNIT.getDesc());
    }

    /**
     * 获取商品规格
     * @return
     */
    public List<Map<String,Object>> getGoodsSpecList(){
        return CommonDao.getInstance().getCommonContentByType(CommonType.SPEC.getDesc());
    }

    /**
     * 获取城市区域列表
     * @return
     */
    public List<City> getCityAreaList(){
        List<Area> areas = CommonDao.getInstance().getAreaList();

        List<City> cities = new ArrayList<City>();

        City city = new City();

        for(Area area : areas){
            if(area.getParent_id()==0){
                city = new City();
                city.setArea_id(area.getArea_id());
                city.setArea_name(area.getArea_name());
                city.setArea_no(area.getArea_no());
                city.setOrder(area.getOrder());
                city.setParent_id(0);
                city.setStatus(area.getStatus());
                cities.add(city);
            }
        }

        List<Area> tempList = new ArrayList<Area>();
        for(City temp : cities){
            tempList = new ArrayList<Area>();
            for(Area area : areas){
                if(Objects.equals(area.getParent_id(), temp.getArea_id())){
                    tempList.add(area);
                }
            }
            temp.setAreaList(tempList);
        }

        return cities;
    }

   /**
    * 功能 : 获取城市列表品
    * 返回值 : List<City>
    * 作者 : Max
    * 日期 : 2017/9/4 13:56
    */
    public List<City> getCityList(){
        return CommonDao.getInstance().getCityList();
    }




    /**
     * 获取渠道列表
     * @return List<Map<String,Object>>
     * @author Max
     * @since 2017/9/11
     */
    public List<Map<String,Object>> getChannelList(){
        return CommonDao.getInstance().getDictionaryList(CommonType.CHANNEL.getDesc());
    }

    /**
     * 获取区域编号与城市编号的对应关系
     * @return Map<String,String>
     * @author Max
     * @since 2017/9/11
     */
    public Map<String,String> getAreaToCity(){
        return CommonDao.getInstance().getAreaToCity();
    }


    public static void main(String[] args) {
        System.out.println(JSON.toJSONString(CommonService.getInstance().getCityAreaList()));
    }

}
